﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Configuration;

namespace UCenterLib {
    delegate void UCApi();

    internal class UCConfig {
        // 直接定义
        public static bool IN_UC = false;
        public static string UC_CLIENT_VERSION = "1.5.0";
        public static string UC_CLIENT_RELEASE = "20090121";
        // public static UCApi UC_API_FUNC = null;

        public static Hashtable cache = new Hashtable();

        // 读取配置文件
        public static string UC_CONNECT {
            get {
                return ReadString("UC_CONNECT");
            }
        }

        public static string UC_DBHOST {
            get {
                return ReadString("UC_DBHOST");
            }
        }

        public static string UC_DBUSER {
            get {
                return ReadString("UC_DBUSER");
            }
        }

        public static string UC_DBPW {
            get {
                return ReadString("UC_DBPW");
            }
        }

        public static string UC_DBNAME {
            get {
                return ReadString("UC_DBNAME");
            }
        }

        public static string UC_DBCHARSET {
            get {
                return ReadString("UC_DBCHARSET");
            }
        }

        public static string UC_DBTABLEPRE {
            get {
                return ReadString("UC_DBTABLEPRE");
            }
        }

        public static bool UC_DBCONNECT {
            get {
                return ReadBoolean("UC_DBCONNECT");
            }
        }

        public static string UC_KEY {
            get {
                return ReadString("UC_KEY");
            }
        }

        public static string UC_API {
            get {
                return ReadString("UC_API");
            }
        }

        public static string UC_CHARSET {
            get {
                return ReadString("UC_CHARSET");
            }
        }

        public static string UC_HOST {
            get {
                return ReadString("UC_HOST");
            }
        }

        public static int UC_APPID {
            get {
                return ReadInt32("UC_APPID");
            }
        }

        public static int UC_PPP {
            get {
                return ReadInt32("UC_PPP");
            }
        }

        /* 自定义配置 */
        /* UC缓存 */
        //public static bool UC_UseCache {
        //    get {
        //        return ReadBoolean("UC_UseCache");
        //    }
        //}

        //public static string UC_CacheType {
        //    get {
        //        return ReadString("UC_CacheType");
        //    }
        //}

        //public static string UC_CacheSqlServer_ConnStr {
        //    get {
        //        return ReadString("UC_CacheSqlServer_ConnStr");
        //    }
        //}

        public static string ReadString(string key){
            lock (cache) {
                // check cache or read from config
                if (cache.ContainsKey(key)) {
                    return Convert.ToString(cache[key]);
                }
                else {
                    string v = ConfigurationManager.AppSettings[key];
                    if (string.IsNullOrEmpty(v)) {
                        // throw unreadable exception if can't find a value from config
                        throw new ConfigurationErrorsException(string.Format("UCenter未正确配置. {0}配置不正确.", key));
                    }
                    else {
                        cache.Add(key, v);
                        return v;
                    }
                }
            }
        }

        public static int ReadInt32(string key) {
            lock (cache) {
                if (cache.ContainsKey(key)) {
                    return Convert.ToInt32(cache[key]);
                }
                else {
                    string vStr = ConfigurationManager.AppSettings[key];
                    int v;
                    if (string.IsNullOrEmpty(vStr) ||
                        !int.TryParse(vStr, out v)) {
                        // throw unreadable exception if can't find a value from config
                        throw new ConfigurationErrorsException(string.Format("UCenter未正确配置. {0}配置不正确.", key));
                    }
                    else {
                        cache.Add(key, v);
                        return v;
                    }
                }
            }
        }

        public static bool ReadBoolean(string key) {
            lock (cache) {
                if (cache.ContainsKey(key)) {
                    return Convert.ToBoolean(cache[key]);
                }
                else {
                    string vStr = ConfigurationManager.AppSettings[key];
                    bool v;
                    if (string.IsNullOrEmpty(vStr) ||
                        !bool.TryParse(vStr, out v)) {
                        // throw unreadable exception if can't find a value from config
                        throw new ConfigurationErrorsException(string.Format("UCenter未正确配置. {0}配置不正确.", key));
                    }
                    else {
                        cache.Add(key, v);
                        return v;
                    }
                }
            }
        }

        //TODO: Throw uc not correctly configured when it is proper
    }
}
